package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.itlong.cloud.POJO.DTO.property.PropertyProjectRemoveUnitInfoDTO;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.property.service.IPropertyUnitInfoService;
import com.itlong.cloud.utils.node.JsonUtil;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * <desc>
 * 项目服务-删除单元业务操作队列消费者。.
 * </desc>
 *
 * @createDate 2017-10-25
 */
@Component
public class RemoveUnitConsumer {

    private static final Logger LOG = LoggerFactory.getLogger(RemoveUnitConsumer.class); //日志对象

    @Autowired
    IPropertyUnitInfoService iPropertyUnitInfoService; //单元信息处理接口


    /**
     * <desc>
     * 项目服务-删除单元业务消费过程.
     * </desc>
     *
     * @param message 监听到的业务消息
     * @param channel 频道
     * @author LewisLouis
     * @createDate 2017-10-25
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_PROJECT_REMOVE_UNIT_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException,IOException {
        byte[] body = message.getBody();
        try {

            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JsonUtil.toObject(new String(body),
                    TimeTaskDeadMessageQueuePO.class);
            String deleteInfo = timeTaskDeadMessageQueuePO.getMessageContent();

            PropertyProjectRemoveUnitInfoDTO propertyProjectRemoveUnitInfoDTO =
                    JSON.parseObject(JSON.parseObject(deleteInfo).getString("data"), PropertyProjectRemoveUnitInfoDTO.class);
            //PropertyProjectRemoveUnitInfoDTO propertyProjectRemoveUnitInfoDTO = JSON.parseObject(deleteInfo, PropertyProjectRemoveUnitInfoDTO.class);

            iPropertyUnitInfoService.removeUnitInfo(propertyProjectRemoveUnitInfoDTO);


        } catch (Exception ex) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),false);
            LOG.error("【RabbitMQ_项目服务-删除单元业务消费端】:消费异常", ex);
        }
        finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
